const fbPassword = "aabb3344";
mainEntrence();

// 程序主入口
function mainEntrence() {
  deviceInfo();
  eventListener();

  sleep(800);

  // 选关卡
  //beforeRun();
  //while (true) {
  // 运行主函数
  main();
  //}
}


function eventListener() {
  threads.start(function () {
    // 启用按键监听
    events.observeKey();
    // 监听音量上键按下
    events.onKeyDown("volume_down", function (event) {
      toastLog("程序手动退出");
      threads.shutDownAll();
      exit();
    });
  });
}


function deviceInfo() {
  auto();
  // if (!requestScreenCapture()) {
  //   toast('请求截图失败，程序结束');
  //   exit();
  // }
  // 调整屏幕亮度
  // device.setBrightness(0);
}

function main() {
  toast("开始运行");

  clearGoogleId();
  sleep(1000);
  clearAsphaltData();
  sleep(1000);
  launcherSSR();
  sleep(1000);
  launcherAsphalt9();

  // 跑完之后

}

function clearGoogleId(){
  launch("com.android.settings");
  sleep(3000);
  for(i=0;i<3;i++){
    swipe(720,2000,720,1000,500);
  }
  sleep(1000);
  click(1000,1000);
  sleep(2000);
  click(1000,2760);
  sleep(700);
  click(1000,400);
  sleep(700);
  click(1200,1700);
  home();
}

function clearAsphaltData(){
  app.openAppSetting("com.gameloft.android.ANMP.GloftA9HM")
  sleep(10000);
  click(1000,1900);
  sleep(700);
  click(400,700);
  sleep(700);
  click(1000,2780);
  sleep(700);
  back();
  home();
}

function launcherSSR(){
  notifications();
  sleep(1500);
  click(1300,380);
  sleep(6000);
  back();
  sleep(500);
}

function launcherAsphalt9(){
  launch("com.gameloft.android.ANMP.GloftA9HM");
  sleep(15000);
  
  //初始年龄性别设置界面
  var started = false;
  do{
    sleep(5000);
    requestScreenCapture();
    var img = captureScreen();
    var manBtn = getPointColor(img,1100,640);
    toast(colors.toString(manBtn));
    if(colors.isSimilar(manBtn,"#ffffffff")){
      toast("开始输入年龄");
      started = true;
    }
  } while(!started);
  sleep(800);
  click(1490,380);
  sleep(1000);
  input("22");
  back();
  sleep(600);
  click(1100,640);
  click(1500,860);
  sleep(3000);

  //加载登录
  var inited = false;
  do{
    sleep(3000);
    var img = captureScreen();
    var fbBtn = getPointColor(img,800,1000);
    toast(colors.toString(fbBtn));
    if(colors.isSimilar(fbBtn,"#ffffffff")){
      toast("开始登录");
      click(800,1000);
      inited = true;
    }
  } while(!inited);

  //开始登录fb
  var loaded = false;
  do{
    sleep(3000);
    var img = captureScreen();
    var bg = getPointColor(img,1200,600);
    toast(colors.toString(bg));
    if(colors.isSimilar(bg,"#ffedeff6",8)){
      toast("开始输入账号密码");
      sleep(1000);
      input(0,"18046043362");
      input(1,fbPassword);
      loaded = true;
    }
  } while(!loaded);
  sleep(800);
  click(1600,1250);

  //确认继续登录
  var logined = false;
  do{
    sleep(3000);
    var img = captureScreen();
    var btnContinue = getPointColor(img,1400,1040);
    toast(colors.toString(btnContinue));
    if(colors.isSimilar(btnContinue,"#ff3578e5",8)){
      click(1400,1040);
      logined = true;
    }
  } while(!logined);

  //选择账号B
  var choosed = false;
  do{
    sleep(3000);
    var img = captureScreen();
    var leftBtn = getPointColor(img,400,1300);
    toast(colors.toString(leftBtn));
    if(colors.isSimilar(leftBtn,"#ffc3fb12",6)){
      click(2000,1300);
      sleep(800);
      click(1000,1120);
      choosed = true;
    }
  } while(!choosed);


}

function race(){
  click(900,1250);
  sleep(2000);
  click(2550,1030);
  sleep(2500);
  click(350,1100);   //选择比赛
  sleep(2000);
  clickContinue();  //进入比赛
  sleep(3000);
  swipe(1000,720,1880,720,500);
  sleep(1500);
  click(firstCarX,firstCarY);

}


function career(){
  for(i = 0;i<5;i++){
    swipe(1480,220,1480,1220,500);
  }
  sleep(1000);
  click(1045,270);
  sleep(1000);
  clickContinue();
  sleep(3000);

  var img = captureScreen();
  var selected = false;

  selected = selectCar(img,firstCarX + 3 * distanceX,firstCarY + distanceY);
  if(!selected){
    selected = selectCar(img,firstCarX + 3 * distanceX,firstCarY);
  }

  if(!selected){
    selected = selectCar(img,firstCarX + 2 * distanceX,firstCarY + distanceY);
  }

  if(!selected){
    selected = selectCar(img,firstCarX + 2 * distanceX,firstCarY);
  }

  if(!selected){
    selected = selectCar(img,firstCarX + distanceX,firstCarY + distanceY);
  }

  if(!selected){
    selected = selectCar(img,firstCarX + distanceX,firstCarY);
  }

  if(!selected){
    selected = selectCar(img,firstCarX,firstCarY + distanceY);
  }

  if(!selected){
    selected = selectCar(img,firstCarX,firstCarY);
  }

  sleep(2000);

  // 开始
  click(2400, 1280);

  sleep(10000);

  // 开跑
  threads.start(function () {
    run()
  });
  
}

function selectCar(img,x,y){
  var oil = images.pixel(img, checkForResolution(x, y, img).positionX, checkForResolution(x, y, img).positionY);
  if (colors.isSimilar(oil, "#ffc0f814")) {
    click(x, y);
    return true;
  }
  return false;
}

function startReady(){
  var img = captureScreen();
  var leftBtn = images.pixel(img, checkForResolution(800, 1000, img).positionX, checkForResolution(800, 1000, img).positionY);
  var rightBtn = images.pixel(img, checkForResolution(1630, 1000, img).positionX, checkForResolution(1630, 1000, img).positionY);
  if(colors.isSimilar(leftBtn, "#ffc3fb12")){
    if(colors.isSimilar(rightBtn,"#ffffffff")){
      toast("点击看广告");
      sleep(1000);
      click(1900,1000);
      adPlay();
    }else{
      sleep(1000);
      startReady();
    }
  }else{
    stop();
  }
}

function afterRun() {
  toast("跑完了");
  sleep(1000);
  clickContinue();
  sleep(2000);
  // var img = captureScreen();
  // var continueBtn = images.pixel(img, checkForResolution(2280, 1260, img).positionX, checkForResolution(2280, 1260, img).positionY);
  // if (colors.isSimilar(continueBtn, "#ffc3fb12")) {
    clickContinue();
    sleep(4000);
    clickContinue();
    sleep(1500);
    clickContinue();
    sleep(4000);
  // } else {
  //   sleep(5000);
  // }

  // reward()

  career();

}

function run() {

  // 定时点击氮气
  var hoop = true;
  while (hoop) {
    var img = captureScreen();
    var continueBtn = images.pixel(img, checkForResolution(2280, 1260, img).positionX, checkForResolution(2280, 1260, img).positionY);
    if (colors.isSimilar(continueBtn, "#ffc3fb12")) {
      afterRun();
      hoop = false;
    } else {
       // swipe(1660,720,1000,720,500);
      click(2490, 1120);
      sleep(800);
    }
  }
}

function clickContinue() {
  click(2280, 1260);
}

function isRunStart(){
  var img = captureScreen();
  var timeClock = images.pixel(img, checkForResolution(2430, 220, img).positionX, checkForResolution(2430, 220, img).positionY);
  isStarted = colors.isSimilar(timeClock,"#ffd90d52");
  
}

function checkForResolution(x, y, img) {
  var width = img.getWidth();
  var height = img.getHeight();
  if (width < height) {
    var e = x;
    x = y;
    y = e;
  }

  return {
    positionX: x,
    positionY: y
  };
}

function getPointColor(img,x,y){
  var data = checkForResolution(x, y, img);
  return images.pixel(img, data.positionX, data.positionY);
}